import os
import re
import requests
import json
import xlsxwriter, xlrd
import csv
import sys, os.path
header = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding':'gzip, deflate',
            'Accept-Language':'zh-CN,zh;q=0.9',
            'Cache-Control':'max-age=0',
            'Connection':'keep-alive',
            'Cookie':'x=1440; JSESSIONID=83DE0D2DB21940B1BBF49AD2DC7963A8.jvm13; username=1218055; LtpaToken2=SI5/AKnUzBA7BvkFunWh6R79v3cw9WW/to3zdL6hC/cqLab3uFrDY2CzoLf4ZWAY2pTLF4vo80ckQ6aNrRXEWYvarhe9rtEl5LSkgXqvixAJM7zH163j03JN+4hqoWXGGZuHGCanFz3hAQiBF8sD63YEXqFGVEe5w3bQ/RrjanyqJJ9kf1whNIUP5C/Hy5FD9buJDKTFdiPR7osyEoVOmKOqKr9bms6OIebQJParUzCi123YrU78m3gzRP7WaKjesCD7kXp4Ff+xA/RpXQjCNT7icuo5uvaAG6Tt909MXCgnOgtvmvllHK/xulQo8Ui7VnXG7FUCsPst0D4emfY0Cv//LQ/PrU0sbIIvwhgfbPKh4BPGiN3lJbPQccd+KGuuYFk6Hr470X/TtujamBiRq+i4uTabACBTPebpbd2mgQgV1ZzgM7SEAUgxun5OOh/SSHyHZQl6LnTo1zZS7FJkgAaJYjkPAx2lDYupJ0Fk88QiCeU0xLbQJXm783FiU7f81YzMvFBgAnVgotwJ3BBFLdYKdfbrTkZUlHIx31/q2NSrUw2wBuLAce5mxoOjKyYs3O/czEawiqCZ6onAbTIGngcUOwxGimgv9StFGAFRptEOHOOBgB/AOR+5Abh9Ru/SqXFXHZFJQtV3S1XPvz4PyLv7ymWITDsSgzW5xt8i/pU=; LtpaToken=cxGpMsWRU3wII3D4JWPpi7kAwXWtVyfTD+s8jMxZg6RijBlI/5M9HtkqR99qxozcpf3fKu+YRSa4Jk63tA+0AONsdnydbeMWHxTzUvJTdGRUC/sIgjcgYgegh+W5QPs2c/zs06oqDMKbfKDKYUz9IsGyhG7vu0wbIPMgJtSWZbFYVgljS3OS92vZaSsK5pWvR2RFlTI3kH4VSvkbgAOM+q/dK5jdDoHmyMu1GvsRJrP1Jb/ASayqIcgacy92eCi3FP8rpS75u0FlcFQVnSnZfwaFdU1/O9j5vRfVOhHubejL55fVeuJ0FLztve64AixIXPtjccylv/asWUYzV3hVptUcp52Rt362; user_id=1218055; access=Home; language=cn; JSESSIONID=0000eK8twvUN5-AsID0tZfPn56j:1b32q9ft6; BROPEN_INTERNAL="U2FsdGVkX19/DYvrQJlB8RLFnprxKhmy7raPAaCxxxzdMphB4CSNz0Bhragq+oY77q2/DJ5eo/sOoRTVyIYaYw=="; BROPEN_INTERNAL="U2FsdGVkX1/uTwVht8EWeids4+51vpggG4G+9SFDRj436q8YUabQMVEG3i90LNFIhILN3LY2jlEUYM6h8HptDw=="',
            'Host':'bpm.boe.com.cn',
            'Referer':'http://portal.boe.com.cn/wps/myportal/Home/home/!ut/p/z1/04_Sj9CPykssy0xPLMnMz0vMAfIjo8zizQwMnD0sTIx83Y2CnQ0cjUPCDIyNvY0MDAz0wwkpiAJKG-AAjiD9UViUOBo4BRk5GRsYuPsbYVWAYkZBboRBpqOiIgBq7zMq/dz/d5/L2dBISEvZ0FBIS9nQSEh/',
            'Upgrade-Insecure-Requests':'1',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}

def get_list(oid):
    global header
    url='http://bpm.boe.com.cn/boeHQ/osm/treeAsJSON?rootContained=true&title=true&ei=true&ecode=true&depth=null&type=1&lazy=true&_=5073522&fragment={}'.format(oid)
    r = requests.get(url,headers=header)
    w=json.loads(r.text)
    for i in w:
        if i[1]['t']=='o':
            for j in i:
                if isinstance(j,list):
                    if isinstance(j[1],dict):
                        if 'lazy' in j[1].keys():
                            get_list(j[1]['id'])
                            f= open('组织清单.txt', "a")
                            f.write(str(j[1]['fn']) + '\n')
                            f.close()
                        elif j[1]['t']=='e':
                            get_data_txt(j[1]['id'])
                            g= open('特殊岗位清单.txt', "a")
                            g.write(str(j[0]) + '\n')
                            g.close()
        elif i[1]['t']=='e':
            get_data_txt(i[1]['id'])
            e = open('清单.txt', "a")
            e.write(str(i[0]) + '\n')
            e.close()

def get_data_txt(pid):
    global header
    f=open('信息.txt', "a")
    try:
        print(pid)
        url='http://bpm.boe.com.cn/boeHQ/app/getEhrEmployeeByEIAsJSON?eiId={}'.format(pid)
        r = requests.get(url,headers=header)
        w=json.loads(r.text)
        for i in w:
            try:
                f.write(w[i]+',')
            except:
                f.write('无,')
    except:
        print('失败 ')
        e = open('错误id.txt', "a")
        e.write(str(pid)+'获取信息失败\n')
        e.close()
    f.write( ';\n')
    f.close()

get_list('5839784')